# -*- coding: utf-8 -*-


# cmap2owl -- Helper application to convert from concept maps to OWL ontologies
# Copyright (c) 2008-2013  Rodrigo Rizzi Starr
#  
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


'''


@author: Rodrigo Rizzi Starr
@copyright: Copyright © 2008-2013 Rodrigo Rizzi Starr
@license: MIT License
@contact: rodrigo.starr@gmail.com
'''

from datetime import datetime
from math import floor

class TimeIt:
    def start(self):
        self.begin = datetime.now()
    
    def finish(self):
        self.end = datetime.now()

    def message(self, message='Operation took'):
        self.finish()
        print '%s: %s' % (message, str(self))
        print
    
    def __str__(self):
        delta = self.end - self.begin
        hours = floor(delta.seconds/3600)
        seconds = delta.seconds % 3600
        minutes = floor(seconds/60)
        seconds = seconds % 60
    
        return ("%d days, %d hours, %d minutes and %d.%d seconds" %
                (delta.days, hours, minutes, seconds, delta.microseconds))


def printTime(message='', func=lambda: True):
    '''Runs func (that should not have any argument) and shows a custom message
       with the time it took to run it.
    '''

    if len(message) == 0:
        message = 'Operation took'

    sw = TimeIt()
    sw.start()
    result = func()
    sw.finish()
    print '%s: %s' % (message, str(sw))
    print

    return result

_timeRef = TimeIt()
def tic():
    'Replicate the tic/toc function in Matlab.'
    _timeRef.start()

def toc():
    _timeRef.finish()
    print 'Elapsed time is ' + str(_timeRef)


